
import joblib
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
# ROC曲线
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
df = pd.read_excel('数据/特征筛选—血脉扩张-前20特征.xlsx')

# df = df.drop(columns='id')
# 输出标签
label = pd.read_excel('数据/1-b-训练.xlsx')
label = label['是否发生血肿扩张']
print('label', label)
# 输入变量
variable = df
print('variable', variable)

label_column = '血肿扩张'

accuracy_score_s=[]
precision_score_s=[]
# ==========归一化============================
def df_mm(df):
    from sklearn.preprocessing import MinMaxScaler

    print('>>>>>>> 数据归一化')

    scaler = MinMaxScaler()
    df = scaler.fit_transform(df)

    return df


variable = df_mm(variable)

# ===============================================数据集分割===========================
train_x = variable[:100, :]
train_y = label[:100]
test_x = variable[100:, :]
test_y = label[100:]

print('训练集\n', train_x.shape, train_y.shape)  # (80, 108) (80,)
print('测试集\n', test_x.shape, test_y.shape)  # (20, 108) (20,)
n=41
m=1

model = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{n}_m{m}.pkl')
predict = model.predict(test_x)
# 转np
test_y = np.array(test_y)
predict = np.array(predict)
real = test_y
accuracy_score = accuracy_score(real, predict)
precision_score = precision_score(real, predict)
recall_score = recall_score(real, predict)
f1_score = f1_score(real, predict)

print('准确率: %.4f' % accuracy_score)
print('精确率: %.4f' % precision_score)
print('召回率、: %.4f' % recall_score)
print('F1 score: %.4f' % f1_score)  # normalize=False 百分比
# fpr, tpr, thersholds = roc_curve(real, predict)
# roc_auc = auc(fpr, tpr)
# plt.plot(fpr, tpr, 'k--', label='ROC (area = {0:.2f})'.format(roc_auc), lw=2)
#
# plt.xlim([-0.05, 1.05])  # 设置x、y轴的上下限，以免和边缘重合，更好的观察图像的整体
# plt.ylim([-0.05, 1.05])
# plt.xlabel('False Positive Rate')
# plt.ylabel('True Positive Rate')  # 可以使用中文，但需要导入一些库即字体
# plt.title('ROC Curve')
# plt.legend(loc="lower right")
# plt.show()
from sklearn.metrics import roc_curve, roc_auc_score
RF_model = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{1}_m{1}.pkl')
RF_model1 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{5}_m{1}.pkl')
RF_model2 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{10}_m{1}.pkl')
# RF_model3 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{10}_m{3}.pkl')
RF_model4 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{20}_m{1}.pkl')
RF_model5 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{30}_m{1}.pkl')
RF_model6 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{41}_m{1}.pkl')
RF_model7 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{41}_m{3}.pkl')
RF_model8 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{41}_m{5}.pkl')
RF_model9 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{41}_m{7}.pkl')
RF_model10 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{41}_m{9}.pkl')
RF_model11 = joblib.load(f'模型/预测模型-血肿扩张-RF_优化_n{41}_m{11}.pkl')
RF_model_proba = RF_model.predict_proba(test_x)[:,1]
RF_model_proba1 = RF_model1.predict_proba(test_x)[:,1]
RF_model_proba2 = RF_model2.predict_proba(test_x)[:,1]
# RF_model_proba3 = RF_model3.predict_proba(test_x)[:,1]
RF_model_proba4 = RF_model4.predict_proba(test_x)[:,1]
RF_model_proba5 = RF_model5.predict_proba(test_x)[:,1]
RF_model_proba6 = RF_model6.predict_proba(test_x)[:,1]
RF_model_proba7 = RF_model7.predict_proba(test_x)[:,1]
RF_model_proba8 = RF_model8.predict_proba(test_x)[:,1]
RF_model_proba9 = RF_model9.predict_proba(test_x)[:,1]
RF_model_proba10 = RF_model10.predict_proba(test_x)[:,1]
RF_model_proba11 = RF_model11.predict_proba(test_x)[:,1]
test_y = np.array(test_y)

# roc_s=[RF_model_proba,RF_model_proba1,RF_model_proba2,RF_model_proba3,RF_model_proba4,RF_model_proba5,RF_model_proba6,RF_model_proba7,RF_model_proba8,RF_model_proba9,RF_model_proba10,RF_model_proba11]
# for r in roc_s:
#
#     fpr, tpr, thresholds = roc_curve(test_y, r)
#     roc_auc = roc_auc(fpr, tpr)
#     plt.plot(fpr, tpr,  label=' ROC curve (area = %0.2f)' % roc_auc)
plt.figure(figsize=(12, 6))
fpr, tpr, thresholds = roc_curve(test_y, RF_model_proba)
roc_auc = roc_auc_score(test_y, RF_model_proba)
plt.plot(fpr, tpr,  label=' n=1,m=1 (area = %0.2f)' % roc_auc)

fpr1, tpr1, thresholds1 = roc_curve(test_y, RF_model_proba1)
roc_auc1 = roc_auc_score(test_y, RF_model_proba1)
plt.plot(fpr1, tpr1,  label=' n=5,m=1 (area = %0.2f)' % roc_auc1)

fpr2, tpr2, thresholds2 = roc_curve(test_y, RF_model_proba2)
roc_auc2 = roc_auc_score(test_y, RF_model_proba2)
plt.plot(fpr2, tpr2,  label=' n=10,m=1 (area = %0.2f)' % roc_auc2)

fpr3, tpr3, thresholds3 = roc_curve(test_y, RF_model_proba4)
roc_auc3 = roc_auc_score(test_y, RF_model_proba4)
plt.plot(fpr3, tpr3,  label=' n=20,m=1 (area = %0.2f)' % roc_auc3)
fpr4, tpr4, thresholds4 = roc_curve(test_y, RF_model_proba5)
roc_auc4 = roc_auc_score(test_y, RF_model_proba5)
plt.plot(fpr4, tpr4, label=' n=30,m=1 (area = %0.2f)' % roc_auc4)
fpr5, tpr5, thresholds5 = roc_curve(test_y, RF_model_proba6)
roc_auc5 = roc_auc_score(test_y, RF_model_proba6)
plt.plot(fpr5, tpr5,  label=' n=41,m=1 (area = %0.2f)' % roc_auc5)
fpr6, tpr6, thresholds6 = roc_curve(test_y, RF_model_proba7)
roc_auc6 = roc_auc_score(test_y, RF_model_proba7)
plt.plot(fpr6, tpr6,  label=' n=41,m=3 (area = %0.2f)' % roc_auc6)
fpr7, tpr7, thresholds7 = roc_curve(test_y, RF_model_proba8)
roc_auc7 = roc_auc_score(test_y, RF_model_proba8)
plt.plot(fpr7, tpr7,  label=' n=41,m=5 (area = %0.2f)' % roc_auc7)
fpr8, tpr8, thresholds8 = roc_curve(test_y, RF_model_proba9)
roc_auc8 = roc_auc_score(test_y, RF_model_proba9)
plt.plot(fpr8, tpr8,  label=' n=41,m=7 (area = %0.2f)' % roc_auc8)

fpr9, tpr9, thresholds9 = roc_curve(test_y, RF_model_proba10)
roc_auc9 = roc_auc_score(test_y, RF_model_proba10)
plt.plot(fpr9, tpr9,  label=' n=41,m=9 (area = %0.2f)' % roc_auc9)
fpr10, tpr10, thresholds10 = roc_curve(test_y, RF_model_proba11)
roc_auc10= roc_auc_score(test_y, RF_model_proba11)
plt.plot(fpr10, tpr10,  label=' n=41,m=11 (area = %0.2f)' % roc_auc10)


plt.plot([0, 1], [0, 1], color='navy', linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])

plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('RF_model (ROC) curve')
plt.legend(loc="lower right")
plt.show()
plt.yticks(fontproperties='Times New Roman')  # size = 14
plt.xticks(fontproperties='Times New Roman')
plt.grid(linestyle='-.')  # 虚线横竖网格
plt.savefig(f'图像/RF模型-参数优化ROC曲线对比')